package com.whfp.coky.ui.adpter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.annotation.ArrayRes
import androidx.annotation.LayoutRes

/**
 *
 *
 * created by coky on 2022/3/18
 */
class SpinnerAdapter<T>(context: Context, resource: Int, objects: Array<T>) :
    ArrayAdapter<T?>(context, resource, objects) {
    internal var selectedPostion = 0
       set(value) {
            field = value
        }


    override fun getDropDownView(
        position: Int,
        convertView: View?,
        parent: ViewGroup
    ): View {
        val view = super.getDropDownView(position, convertView, parent)
        val textView = view as TextView
        if (selectedPostion == position) {
            textView.setTextColor(-0xc8c8bf)
            textView.paint.isFakeBoldText = true
        } else {
            textView.setTextColor(-0x929293)
            textView.paint.isFakeBoldText = false
        }
        return view
    }

    companion object {
        fun createFromResource(
            context: Context,
            @ArrayRes textArrayResId: Int, @LayoutRes textViewResId: Int
        ): SpinnerAdapter<CharSequence> {
            val strings = context.resources.getTextArray(textArrayResId)
            return SpinnerAdapter(context, textViewResId, strings)
        }
    }
}
